import { defineStore } from 'pinia'
import { ref } from 'vue'
import axios from 'axios'

export const useInfoStore = defineStore('info', () => {
  const infoList = ref([])
  const userActions = ref([])
  
  const fetchInfoList = async () => {
    try {
      const res = await axios.get('/api/info/')
      infoList.value = res.data
    } catch (error) {
      console.error('获取信息列表失败:', error)
    }
  }
  
  const fetchUserActions = async () => {
    try {
      const res = await axios.get('/api/user-actions/')
      userActions.value = res.data
    } catch (error) {
      console.error('获取用户行为失败:', error)
    }
  }
  
  const addAction = async (infoId, actionType) => {
    try {
      await axios.post('/api/user-actions/', {
        information: infoId,
        action_type: actionType
      })
      await fetchUserActions()
    } catch (error) {
      throw error
    }
  }
  
  const removeAction = async (infoId, actionType) => {
    try {
      await axios.delete(`/api/user-actions/?information=${infoId}&action_type=${actionType}`)
      await fetchUserActions()
    } catch (error) {
      throw error
    }
  }
  
  return {
    infoList,
    userActions,
    fetchInfoList,
    fetchUserActions,
    addAction,
    removeAction
  }
})